<template>
  <div class="container">
    <div class="com-page" ref="stock_ref"></div>
  </div>
</template>

<script>
export default {
  name: "Stock",
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0,
      timerId: null
    }
  },
  created() {
    this.$socket.registerCallBack('stockData',this.getData)
  },
  mounted() {
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: ''
    })
    window.addEventListener('resize', this.screenAdapter)
    this.startInterval()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('stockData')
    clearInterval(this.timerId)
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, "chalk")
      const initOptions = {
        title: {
          text: '库存和销量分析',
          left: 20,
          top: 20
        },
        textStyle: {
          fontSize: 18
        }
      }
      this.chartInstance.setOption(initOptions)
      this.chartInstance.on('mouseover', ()=> {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', ()=> {
        this.startInterval()
      })
    },
    getData(resData) {
      this.allData = resData
      this.updateChart()
    },
    updateChart() {
      const centerPositionArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4ff778', '#0ba82c'],
        ['#e5dd45', '#e8b11c'],
        ['#e8821c', '#e55445'],
        ['#5052ee', '#ab6ee5'],
        ['#23e5e5', '#2e72bf'],
      ]
      let start = this.currentIndex * 5
      let end =  (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          radius: [30, 50],
          center: centerPositionArr[index],
          hoverAnimation: false,
          labelLine:{
            show: false
          },
          label: {
            position: 'center',
            color: colorArr[index][0]
          },
          data: [
            {
              name: `${item.name}\n ${item.sales}`,
              value: item.sales,
              itemStyle: {
                color:  new this.$echarts.graphic.LinearGradient(0,1,0,0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: 'grey'
              }
            }
          ]
        }
      })
      const updateOptions = {
        series: seriesArr
      }
      this.chartInstance.setOption(updateOptions)
    },
    screenAdapter() {
      const titleFontSize = this.$refs.stock_ref.offsetWidth / 100 * 3.6
      const innerRadius = titleFontSize * 2.8
      const outterRadius = innerRadius * 1.125
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval(){
      this.timerId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1){
          this.currentIndex = 0
        }
        this.updateChart()
      }, 3000)
    }
  }
}
</script>

<style scoped>

</style>